package com.yshebkb.sulp.dao;

import com.yshebkb.sulp.entity.Menu;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author yshebkb
 */
@Repository
@Mapper
public interface MenuMapper {

    @Delete({
            "delete from menu",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into menu (id, name, ",
            "url, order_num, icon, ",
            "parent_id, perms, ",
            "status, type, create_time, ",
            "update_time)",
            "values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
            "#{url,jdbcType=VARCHAR}, #{orderNum,jdbcType=TINYINT}, #{icon,jdbcType=VARCHAR}, ",
            "#{parentId,jdbcType=BIGINT}, #{perms,jdbcType=VARCHAR}, ",
            "#{status,jdbcType=TINYINT}, #{type,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, ",
            "#{updateTime,jdbcType=TIMESTAMP})"
    })
    int insert(Menu record);

    int insertSelective(Menu record);

    @Select({
            "select",
            "id, name, url, order_num, icon, parent_id, perms, status, type, create_time, ",
            "update_time",
            "from menu",
            "where id = #{id,jdbcType=BIGINT}"
    })
    Menu selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Menu record);

    @Update({
            "update menu",
            "set name = #{name,jdbcType=VARCHAR},",
            "url = #{url,jdbcType=VARCHAR},",
            "order_num = #{orderNum,jdbcType=TINYINT},",
            "icon = #{icon,jdbcType=VARCHAR},",
            "parent_id = #{parentId,jdbcType=BIGINT},",
            "perms = #{perms,jdbcType=VARCHAR},",
            "status = #{status,jdbcType=TINYINT},",
            "type = #{type,jdbcType=TINYINT},",
            "create_time = #{createTime,jdbcType=TIMESTAMP},",
            "update_time = #{updateTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Menu record);

    @Select("select * from menu")
    List<Menu> findAll();

    @Select("select * from menu where parent_id=#{parentId}")
    List<Menu> findMenusByParentId(Long parendId);

    @Select("select * from menu where id in (select role_menu.menu_id from role_menu where role_menu.role_id=#{id})")
    List<Menu> selectAllMenus(Long id);
}